package com.sinotn.ks.node.camera.receiver;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.sinotn.SinotnLogger;
import com.sinotn.gson.GsonFacade;
import com.sinotn.ks.definition.Computer;
import com.sinotn.ks.definition.ExamEngine;
import com.sinotn.ks.definition.Examinee;
import com.sinotn.ks.definition.Message;
import com.sinotn.ks.definition.MessageFile;
import com.sinotn.ks.definition.MessageReceiver;
import com.sinotn.ks.netty.message.DefaultMessageFile;
import com.sinotn.ks.netty.message.SimpleMessage;
import com.sinotn.ks.node.camera.dao.cond.CameraPhotoCond;
import com.sinotn.ks.node.camera.service.CameraService;
import com.sinotn.ks.node.camera.vo.CameraPhotoVO;
import com.sinotn.ks.node.runtime.FileStoreUtil;
import com.sinotn.util.FileUtil;
/**
 * 监考录音文件接收
 * @Title MicRecordReceiver.java
 * @Package com.sinotn.ks.node.camera.receiver
 * @Description
 * Copyright: Copyright (c) 2015
 * Company:北京信诺软通
 *
 * @author <a href="mailto:xieqj@sinoth.com">谢启进</a>
 * @date 2020年3月13日 上午10:49:21
 * @version V1.0
 */
public class MicRecordReceiver implements MessageReceiver {
	@Autowired
	private ExamEngine examEngine;
	@Autowired
	private CameraService cameraService;
	private int type=1002;

	@Override
	public int getType() {
		return this.type;
	}

	public void setType(int type) {
		this.type = type;
	}

	@Override
	public void process(Computer computer, Message msg) {
		MessageFile[] files = null;
		try {
			files = msg.getFiles();
			if(files==null || files.length==0){
				SimpleMessage sm = new SimpleMessage(0, msg.getToken());
				HashMap<String, Object> result=new HashMap<String, Object>();
				result.put("method", "camera#playAudio");
				String text=msg.getText();
				if(StringUtils.isNotEmpty(text)){
					CameraPhotoVO vo=this.cameraService.findCameraPhotoById(text);
					if(vo!=null){
						File folder=FileStoreUtil.getStoreFolder(vo.getRoomId(), vo.getExamineeId(), false);
						if(folder!=null){
							File file=new File(folder,  text+".mp3");
							if(file.exists()){
								MessageFile[] fs=new MessageFile[1];
								fs[0]=new DefaultMessageFile("temp\\"+vo.getRoomId()+"\\"+vo.getExamineeId()+"\\"+vo.getId()+".mp3", file);
								sm.setMessageFiles(fs);
								result.put("text", fs[0].getPath());
							}
						}
					}
				}
				sm.setText(GsonFacade.toJson(result));
				computer.sendMessage(sm);
			}else{
				if(this.examEngine.getRunState()==ExamEngine.STATE_STD){
					// 参数解析
					HashMap<?, ?> params = GsonFacade.fromJson(msg.getText(), HashMap.class);
					String examineeId = (String) params.get("examineeId");
					// 通过考试引擎查询考生信息，如果为空则返回
					Examinee examinee = examEngine.getExaminee(examineeId);
					if (examinee != null) {
						CameraPhotoCond cond=new CameraPhotoCond();
						cond.setExamineeId(examineeId);
						List<CameraPhotoVO> list=this.cameraService.findCameraPhotos(1, 1, cond);
						if(list!=null && list.size()>0){
							CameraPhotoVO vo=list.get(0);
							// 默认是只有一个音频文件
							File file = files[0].getFile();
							// 保存文件到home目录，如果不存在则进行创建
							File directory = FileStoreUtil.getStoreFolder(examinee.getRoomId(), examineeId, true);
							File destFile = new File(directory, vo.getId() + ".mp3");
							FileUtil.copyFile(file, destFile);
							// 拷贝照片到ks-rs-web，供管理机访问图片
							File rsDirectory = new File(FileStoreUtil.getPhotoHttpFolder(examinee.getRoomId(), true), examineeId);
							if (!rsDirectory.exists()) {
								rsDirectory.mkdir();
							}
							File rsDestFile = new File(rsDirectory, destFile.getName());
							FileUtil.copyFile(file, rsDestFile);
						}
					}
				}
				// 发送消息给客户端
				SimpleMessage sm = new SimpleMessage(0, msg.getToken());
				computer.sendMessage(sm);
			}
		} catch (Throwable e) {
			SinotnLogger.DEBUG.error("保存考试机录音文件发生系统错误", e);
		} finally {
			if (files != null && files.length > 0) {
				for (int i = 0; i < files.length; i++) {
					files[i].delete();
				}
			}
		}
	}
}
